// pages/calendar/createSchedule/createSchedule.js
const CN_Date = require('../../getCNDate');
import { saveschedule, editScheduleData } from '../../../../api/calendar';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //备注
    getComment: '',
    //已付
    getPaied: '',
    //价格
    getPrice: '',
    //客户
    customerName: '',
    customerPhone: '',
    //地点
    location: '',
    //类型
    dateTypeIndex: 0,
    dateTypeArray: [
      '选择类型',
      '婚礼',
      '寿宴',
      '宝宝宴',
      '活动',
      '彩排',
      '试妆',
      '约见',
      '会议',
      '约拍',
      '其他',
    ],
    //上午颜色
    amColor: '#D3D3D3',
    lunchColor: '#D3D3D3',
    pmColor: '#D3D3D3',
    dinnerColor: '#D3D3D3',
    nightColor: '#D3D3D3',
    //日期
    date: '',
    //农历
    lunar: '',
    //时间
    time: '',
    chinesetime: '',
    //时间段
    time_interval: '',
    //计划id
    scheduleid: '',
    //用户id
    userid: '',
    //全部
    dataList: '',
  },
  //删除计划
  delschedule() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      cancelColor: 'cancelColor',
      success: function(res) {
        if (res.confirm) {
          editScheduleData({
            schedule_id: that.data.scheduleid,
            is_delete: '1',
          }).then(res => {});
          wx.navigateBack({
            delta: 1,
          });
        }
      },
    });
    // editScheduleData({
    //   "schedule_id": this.data.scheduleid,
    //   "is_delete": "1"
    // }).then((res)=>{
    //
    // })
  },
  //备注输入
  getCommentinput(e) {
    this.setData({
      getComment: e.detail.value,
    });
  },
  //已付输入
  getPaiedinput(e) {
    this.setData({
      getPaied: e.detail.value,
    });
  },
  //价格输入
  getPriceinput(e) {
    this.setData({
      getPrice: e.detail.value,
    });
  },
  //电话输入
  customerPhoneinput(e) {
    this.setData({
      customerPhone: e.detail.value,
    });
  },
  //名字输入
  customerNameinput(e) {
    this.setData({
      customerName: e.detail.value,
    });
  },
  //地点输入
  positioninput(e) {
    this.setData({
      location: e.detail.value,
    });
  },
  //保存
  savePlan() {
    if (this.data.chinesetime == '') {
      wx.showToast({
        title: '请输入时间',
        icon: 'none',
      });
      return;
    }

    if (this.data.dateTypeIndex == 0) {
      wx.showToast({
        title: '请选择档期类型',
        icon: 'none',
      });
      return;
    }
    if (this.data.location == '') {
      wx.showToast({
        title: '请输入地址',
        icon: 'none',
      });
      return;
    }
    let time_interval = [];
    time_interval.push(this.data.time);
    time_interval.push(this.data.chinesetime);
    ;
    editScheduleData({
      schedule_id: this.data.scheduleid,
      is_delete: '0',
      date: this.data.date,
      time_interval: time_interval,
      type: this.data.dateTypeIndex.toString(),
      address: this.data.location,
      customer_name: this.data.customerName,
      customer_phone: this.data.customerPhone,
      price: this.data.getPrice,
      paid: this.data.getPaied,
      kind: '1',
      remark: this.data.getComment,
    }).then(res => {
      wx.navigateBack({
        delta: 2,
      });
    });
  },
  //选择类型
  dateTypeChange(e) {
    this.setData({
      dateTypeIndex: e.detail.value,
    });
  },
  //上午颜色
  changeAmColor(e) {
    if (this.data.amColor == '#D3D3D3') {
      this.setData({
        amColor: '#FFC0CB',
        lunchColor: '#D3D3D3',
        pmColor: '#D3D3D3',
        dinnerColor: '#D3D3D3',
        nightColor: '#D3D3D3',
        chinesetime: '上午',
      });
    }
  },
  changeLunchColor(e) {
    if (this.data.lunchColor == '#D3D3D3') {
      this.setData({
        lunchColor: '#FFC0CB',
        amColor: '#D3D3D3',
        // lunchColor:"#D3D3D3",
        pmColor: '#D3D3D3',
        dinnerColor: '#D3D3D3',
        nightColor: '#D3D3D3',
        chinesetime: '午宴',
      });
    }
  },
  changePmColor(e) {
    if (this.data.pmColor == '#D3D3D3') {
      this.setData({
        pmColor: '#FFC0CB',
        amColor: '#D3D3D3',
        lunchColor: '#D3D3D3',
        // pmColor:"#D3D3D3",
        dinnerColor: '#D3D3D3',
        nightColor: '#D3D3D3',
        chinesetime: '下午',
      });
    }
  },
  changeDinnerColor(e) {
    if (this.data.dinnerColor == '#D3D3D3') {
      this.setData({
        dinnerColor: '#FFC0CB',
        amColor: '#D3D3D3',
        lunchColor: '#D3D3D3',
        pmColor: '#D3D3D3',
        // dinnerColor:"#D3D3D3",
        nightColor: '#D3D3D3',
        chinesetime: '晚宴',
      });
    }
  },
  changeNightColor(e) {
    if (this.data.nightColor == '#D3D3D3') {
      this.setData({
        nightColor: '#FFC0CB',
        amColor: '#D3D3D3',
        lunchColor: '#D3D3D3',
        pmColor: '#D3D3D3',
        dinnerColor: '#D3D3D3',
        // nightColor:"#D3D3D3",
        chinesetime: '晚上',
      });
    }
  },
  //日期
  datechange(e) {
    let newarr = e.detail.value.split('-');
    this.setData({
      date: e.detail.value,
      lunar: CN_Date(newarr[0], newarr[1], newarr[2]),
    });
  },
  //时间
  timechange(e) {
    this.setData({
      time: e.detail.value,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    //获取上个页面的监听事件
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('editsaveDate', function(data) {
      that.setData({
        getComment: data.remark,
        getPaied: data.paid,
        getPrice: data.price,
        customerName: data.customer_name,
        customerPhone: data.customer_phone,
        location: data.address,
        dateTypeIndex: data.type,
        date: data.dateReal,
        time: data.time_interval.length > 5 ? data.time_interval.slice(0 ,5) : '',
        chinesetime: data.time_interval.slice(-2),
        scheduleid: data.schedule_id,
        userid: data.userId,
        amColor: data.amColor,
        lunchColor: data.lunchColor,
        pmColor: data.pmColor,
        dinnerColor: data.dinnerColor,
        nightColor: data.nightColor,
        dataList: data,
      });
      if (data.time_interval2 == '上午') {
        that.setData({
          amColor: '#FFC0CB',
          chinesetime: '上午',
        });
      }
      if (data.time_interval2 == '午宴') {
        that.setData({
          lunchColor: '#FFC0CB',
          chinesetime: '午宴',
        });
      }
      if (data.time_interval2 == '下午') {
        that.setData({
          pmColor: '#FFC0CB',
          chinesetime: '下午',
        });
      }
      if (data.time_interval2 == '晚宴') {
        that.setData({
          dinnerColor: '#FFC0CB',
          chinesetime: '晚宴',
        });
      }
      if (data.time_interval2 == '晚上') {
        that.setData({
          nightColor: '#FFC0CB',
          chinesetime: '晚上',
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},
});
